<?php

namespace app\admin\controller;

use app\admin\controller\BaseController;
use think\Request;
use think\Validate;

class DictItem extends BaseController
{
    // 设置模型名称
    protected $modelName = 'dict_item';

    protected $uniqueWithParentFields = ['key'];

    protected $parentIdField = 'dict_id';

    protected $fieldLabels = [
        'key' => '键名',
    ];

    // 设置验证规则
    // 添加自定义string验证规则
    public function __construct()
    {
        parent::__construct();
        Validate::extend('string', function ($value) {
            return is_string($value);
        }, '必须是字符串类型');
    }

    // 设置验证规则
    protected $validateRules = [
        'id' => 'number',
        'dict_id' => 'number',
        'key' => 'require|max:20',
        'value' => 'require|max:100'
    ];

    // 设置规则提示
    protected $validateRulesMsg = [
        'key.require' => '键名不能为空',
        'key.max' => '键名最多20个字符',
        'value.require' => '键值不能为空',
        'value.max' => '键值最多100个字符',
    ];
}
